package com.google.code.jmarias.server.phases;

import com.google.code.jmarias.common.action.ChooseFromPeopleAction;
import com.google.code.jmarias.common.action.DealFiveCardsAction;
import com.google.code.jmarias.common.action.PickedTrumpAction;
import com.google.code.jmarias.common.action.SignedAction;
import com.google.code.jmarias.server.tcp.MariasGame;
import com.google.code.jmarias.common.NewPlayer;
import com.google.code.jmarias.common.NewTable;
import com.google.code.jmarias.common.action.DealFiveHiddenCardsAction;

/**
 *
 * @author Ondrej Michal
 */
public class TrumpPickPhase extends NewAbstractPhase {

    public TrumpPickPhase(NewPlayer playingNow) {
        super(playingNow);
        addListener(new ActionListener(PickedTrumpAction.class) {

            @Override
            protected NewAbstractPhase processAction(SignedAction action, NewTable table) {

                // kontrola vyberu trumfu

                broadcast(action, table.getPlayers());

                broadcast(new SignedAction(new DealFiveCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.forehand).getClient())), table.getPlayer(MariasGame.forehand));
                broadcast(new SignedAction(new DealFiveCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.left).getClient())), table.getPlayer(MariasGame.left));
                broadcast(new SignedAction(new DealFiveCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.right).getClient())), table.getPlayer(MariasGame.right));

                return new TalonDroppingPhase(table.getPlayer(MariasGame.forehand));
            }
        });

        addListener(new ActionListener(ChooseFromPeopleAction.class) {

            @Override
            protected NewAbstractPhase processAction(SignedAction action, NewTable table) {
                
                // neni co kontrolovat

                broadcast(action);

                broadcast(new SignedAction(new DealFiveHiddenCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.forehand).getClient())));
                broadcast(new SignedAction(new DealFiveCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.left).getClient())), table.getPlayer(MariasGame.left));
                broadcast(new SignedAction(new DealFiveCardsAction(table.getDeck().getCards(5), table.getPlayer(MariasGame.right).getClient())), table.getPlayer(MariasGame.right));

                return new ChooseFromPeoplePhase(table.getPlayer(MariasGame.forehand));
            }
        });
    }

    @Override
    public String toString() {
        return "Volba trumfu";
    }


//    /**
//     * sets the suit of trumps
//     * @param action action to be processed
//     * @return the next phase
//     */
//    private AbstractPhase processPickedTrumpAction(PickedTrumpAction action) {
//        table.setTrumpSuit(action.getTrumpSuitCard().getSuit());
//        table.getClients().get(0).sendActionEcho(new PickCardFromPeopleAction(action.getTrumpSuitCard()));
//        table.getClients().get(0).sendActionEcho(new DealFiveCardsAction(table.getFiveCards(2)));
//        return new TalonDroppingPhase();
//    }
//
//    private AbstractPhase processChooseFromPeopleAction(ChooseFromPeopleAction action) {
//        table.getClients().get(0).sendActionEcho(new ChooseFromPeopleAction(5));
//        System.out.println("SERVER CHECK: processChooseFromPeopleAction accepted!");
//        return new ChooseFromPeoplePhase();
//    }


}
